﻿using AutoUpdater.Bll;
using AutoUpdater.Util;
using System;
using System.IO;
using System.Threading;

namespace AutoUpdater
{
    public class CsUpdateSelf
    {
        private string updateExeName;
        public event Action<bool,bool> UpdateCompleted;
        private bool _isRun = false;

        private Thread thread;

        public CsUpdateSelf()
        {
        }

        public void Start()
        {
            _isRun = true;
            thread = new Thread(DealUpdate);
            thread.Start();
        }

        public void Stop()
        {
            _isRun = false;
            if (thread != null)
            {
                try
                {
                    thread.Abort();
                }
                catch (Exception ex) { }
            }
        }

        private void DealUpdate()
        {
            //如果启动的是exe2，复制文件到exe
            //  继续往下执行；
            //1. 获取本地程序版本号
            //2. 获取服务端自动更新程序版本号
            //3. 如果需要更新 
            //      复制一个updateexe2
            //        结束运行重新启动
            //4. 
            //
            string tempPath = ProcessUtil.TempPath;

            //1. 获取本地程序版本号
            string version = VersionXmlUtil.GetUpdateExeVersion();

            FileCheckReq fileCheckReq = new FileCheckReq();
            fileCheckReq.clientVersion = version;
            var contract = FileCheckBll.CheckUpdateExeFile(fileCheckReq);

            if(contract != null)
            {
                VersionXmlUtil.SetUpdateExeCurrentUpdateVersion(contract.clientVersion);

                //if (contract.deleteFiles != null && contract.deleteFiles.Count > 0)
                //{
                //    foreach (var file in contract.updateFiles)
                //    {
                //        string fileName = Path.Combine(directoryPath,file.filePath, file.fileName);
                //        File.Delete(fileName);
                //    }
                //}

                if (contract.updateFiles != null && contract.updateFiles.Count > 0)
                {
                    bool modifyUpdateExe = false;
                    foreach (var file in contract.updateFiles)
                    {
                        if("upateexe2.exe".Equals(file.fileName))
                        {
                            modifyUpdateExe = true;
                        }

                        int start = 0;
                        while (start < file.fileSize)
                        {
                            byte[] fileByte = FileCheckBll.DownLoadFile(file, start);
                            string filename = Path.Combine(tempPath, file.filePath, file.fileName);

                            // 写入文件
                            using(FileStream fileStream = new FileStream(filename, FileMode.OpenOrCreate))
                            {
                                fileStream.Seek(start, SeekOrigin.Begin);
                                fileStream.Write(fileByte, 0, fileByte.Length);
                            };

                            start += fileByte.Length;
                        }
                    }

                    if(modifyUpdateExe)
                    {
                        UpdateCompleted(true, true);
                        return;
                    }
                }
            }
            UpdateCompleted(true, false);
        }
    }
}
